package com.rzjm.exercise.module.controller;

import cn.hutool.core.util.StrUtil;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.ExcelUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.FireWorkRecord;
import com.rzjm.exercise.module.domain.Shop;
import com.rzjm.exercise.module.service.FireWorkRecordService;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 烟花爆竹记录
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2020-12-15 22:36:23
 */
@Controller
@RequestMapping("/module/fire/work/record")
public class FireWorkRecordController extends BaseController {
    @Resource
    private FireWorkRecordService fireWorkRecordService;
    @Resource
    private ShopService shopService;

    @Log("查询烟花爆竹记录页面")
    @GetMapping("/{shopId}")
    @RequiresPermissions("module:firework:record")
    String Shop(Model model,@PathVariable("shopId") Integer shopId) {
        model.addAttribute("shopId",shopId);
        List<Shop> list = shopService.selectListByColumn("shop_type",3);
        model.addAttribute("shops",list);
        return "module/shop/firework/record";
    }

    @Log("查询烟花爆竹记录")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:firework:record")
    public DataTableLayUI<FireWorkRecord> list(DataTableLayUI<FireWorkRecord> condition) {
        DataTableLayUI<FireWorkRecord> dt = fireWorkRecordService.pageSearchLayUI(condition);
        return dt;
    }


    @Log("导出列表")
    @GetMapping("/exportExcel")
    public void testExprotExcel(@RequestParam("begin_date") String begin_date, @RequestParam("end_date") String end_date,
                                @RequestParam("shopId") String shopId, HttpServletRequest request, HttpServletResponse response){
        UserDO user = getUser();
        Map<String,Object> map = new HashMap<>();
        if(user.getUserType() == 0 || user.getUserType() == 1){
            if(StrUtil.isNotEmpty(shopId)){
                map.put("shop_id",Integer.parseInt(shopId));
            }
        } else if(user.getUserType() == 2){//集团管理员
            if(StrUtil.isNotEmpty(shopId)){
                map.put("shop_id",Integer.parseInt(shopId));
            }else{
                map.put("jituan_user_id",user.getUserId());
            }
        }else if(user.getUserType() == 3){//片区管理员
            if(StrUtil.isNotEmpty(shopId)){
                map.put("shop_id",Integer.parseInt(shopId));
            }else{
                map.put("sys_user_id",user.getUserId());
            }
        }
        else if(user.getUserType() == 4 || user.getUserType() == 5 || user.getUserType() == 6){
            map.put("shop_id",user.getShopId());
        }

        if(StrUtil.isNotEmpty(begin_date) && StrUtil.isNotEmpty(end_date)){
            map.put("begin_time",begin_date +" 00:00:00");
            map.put("end_time",end_date +" 23:59:59");
        }
        List<FireWorkRecord> records = fireWorkRecordService.selectByMap(map);
        records.forEach(record->{
            record.setCreateTimeStr(DateUtils.getTime(record.getCreateTime()));
        });
        ExcelUtils.writeExcel(response, records, FireWorkRecord.class, System.currentTimeMillis() + "_烟花爆竹上报记录.xlsx");
    }


    @Log("新增烟花爆竹记录页面")
    @GetMapping("/add")
    @RequiresPermissions("module:firework:add")
    String add(Model model) {
        FireWorkRecord group = new FireWorkRecord();
        model.addAttribute("group", group);
        return "module/shop/firework/detail";
    }


    @Log("编辑烟花爆竹记录页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:firework:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        FireWorkRecord group = fireWorkRecordService.findById(id);
        model.addAttribute("group", group);
        return "module/shop/firework/detail";
    }

    /**
     * 保存
     */
    @Log("新增烟花爆竹记录")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:firework:add")
    public R save(@Valid FireWorkRecord group) {
        if (fireWorkRecordService.save(group)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新烟花爆竹记录")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:firework:edit")
    public R update(@Valid FireWorkRecord group) {
        fireWorkRecordService.updateById(group);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除烟花爆竹记录")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:firework:remove")
    public R remove( Integer id) {
        if (fireWorkRecordService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }
}
